package dl.jf.dao.impl;

import java.util.ArrayList;


import dl.jf.common.constants.Constants;
import dl.jf.common.model.LabelValue;
import dl.jf.common.model.MCode;
import dl.jf.dao.idao.ICommonDao;

public class CommonDaoImpl extends BaseDaoImpl implements ICommonDao {

	@SuppressWarnings("unchecked")
	@Override
	public ArrayList<LabelValue> getCodeLabelListById(int codeId) {
		return (ArrayList<LabelValue>) getSqlMapClientTemplate().queryForList(
				"commonSQL.getCodeLabelListById", codeId);
	}

	@Override
	public String getGenerateCode(int code, int len, String prefix) throws Exception {
		Integer no = (Integer) getSqlMapClientTemplate().queryForObject(
				"commonSQL.getGenerateCode", code);

		if ((++no).toString().length() > len) {
			no = 1;
		}
		MCode model = new MCode();
		model.setCode(code);
		model.setContent(no.toString());
		getSqlMapClientTemplate().update("commonSQL.updateGenerateCode", model);
		return prefix + String.format("%0" + Integer.toString(len) + "d", no);
	}
}
